<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-11-23 14:10
 *
 * Version: 2.0
 */

namespace App\Modules\Coupon\Repositories;


use App\Exceptions\NotFoundException;
use App\Models\CouponList;
use Flower\Core\Repository\Eloquent\BaseRepository;

class CouponListRepository extends BaseRepository
{

	public function model()
	{
		return CouponList::class;
	}

	public function isExisted($couponCodeOrId,$isCode=true){
		$where = $isCode ?  ['code'=>$couponCodeOrId] : ['id'=>$couponCodeOrId];
		$info = $this->model->where($where)->first();
		if(empty($info)){
			throw  new NotFoundException(['msg'=>'优惠券信息不存在！']);
		}
		return $info;
	}


	public function getMemberListByStoreId($storeId, $memberId)
	{
		$where = [
			'store_id'  => $storeId,
			'member_id' => $memberId
		];
		return $this->findWhere($where)->toArray();
	}


	public function add($storeId, $couponId, $memberId, $couponCode,$source,$userId,$liveId, $sendId)
	{
		return $this->create([
			'store_id'  => $storeId,
			'coupon_id' => $couponId,
			'code'      => $couponCode,
			'member_id' => $memberId,
			'send_time' => date('Y-m-d H:i:s'),
			'status'    => 0,
			'source'    => $source,
			'user_id'   => $userId,
			'live_id'   => $liveId,
			'send_id'   => $sendId
		]);
	}



	public function IsExistedByCouponIdAndMemberId($couponId,$memberId,$sendId = 0 ){
		$model = $this->model->where(['coupon_id'=>$couponId,'member_id'=>$memberId]);
		return empty($sendId) ? $model->first() : $model->where(['send_id'=>$sendId])->first();
	}


	public function IsExistedBySendIdAndMemberId($sendId,$memberId){
		return $this->model->where(['send_id'=>$sendId,'member_id'=>$memberId])->first();
	}



	public function getMemberCouponListByStatus($memberId,$status,$pageSize=10){
		$status = isset($status) ? $status : 0;
		return $this->model->with('coupon:id,name,type,money,discount,use_condition,use_start_time,use_end_time')->with(['store'=>function($v){
				return $v->select(['id','logo','name'])->with('picture:id,image');
			}])->where(['member_id'=>$memberId,'status'=>$status])->paginate($pageSize,['*']);
	}


	public function getMemberCouponListCountsByStatus($memberId){
		return $this->model->selectRaw('count(*) as counts, status')->where(['member_id'=>$memberId])->groupBy('status')->get()->toArray();
	}



	/**
	 * 领取用户的列表
	 * @param $storeId
	 * @param $userId
	 * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model[]
	 */
	public function getReceiveMemberCouponList($storeId,$userId){
		return $this->model->select(['id','coupon_id','member_id'])->with('coupon:id,name,type,money,discount','member:id,name,face')->where(['store_id'=>$storeId,'user_id'=>$userId])->get();
	}


	/**
	 * 红包详情
	 * @param $id
	 * @return mixed
	 */
	public function detail($id){
		return $this->model->select(['id','coupon_id','created_at'])->with(['coupon'=>function($v){
			$v->selectRaw('id,name,type,money,discount,use_condition,date(use_start_time) as use_start_time,date(use_end_time) as use_end_time');}])->where(['id'=>$id])->first();
	}


	/**
	 * 抢到优惠券的用户列表
	 * @param $couponId
	 * @return mixed
	 */
	public function getListByCouponId($couponId,$live_id=0){
		$model = $this->model->select(['id','member_id','coupon_id','created_at'])->with('member:id,name,face')->with(['coupon'=>function($v){
			$v->selectRaw('id,name,type,money,discount,use_condition,date(use_start_time) as use_start_time,date(use_end_time) as use_end_time');
		}])->where(['coupon_id'=>$couponId])->where('source',0)->whereDate('created_at','=',date('Y-m-d'));
		return empty($live_id) ? $model->get() : $model->where(['live_id'=>$live_id])->get();
	}


	/**
	 * 已使用未使用的
	 * @param $couponId
	 * @param int $isUsed
	 * @param int $pageSize
	 * @return mixed
	 */
	public function getDetailListByCouponId($couponId,$isUsed=0,$pageSize=10){
		$model = $this->model->select(['id','member_id','coupon_id','created_at','use_time','user_id'])->with('member:id,name,face','user:id,nick')->with(['coupon'=>function($v){
			$v->selectRaw('id,name,type,money,discount,use_condition,date(use_start_time) as use_start_time,date(use_end_time) as use_end_time');
		}])->where(['coupon_id'=>$couponId]);
		if($isUsed == 1){
			$model = $model->where('use_time','!=','0000-00-00 00:00:00');
		}else if($isUsed == 2){
			$model = $model->where('use_time','=','0000-00-00 00:00:00');
		}
		return  $model->paginate($pageSize,['*']);
	}






	public function getMemberSelfListToday($memberId,$userId,$live_id=0){
		$model = $this->model->select(['id','member_id','coupon_id','created_at'])->with('member:id,name,face')->with(['coupon' => function($v) use($userId){
			$v->selectRaw('id,name,type,money,discount,use_condition,date(use_start_time) as use_start_time,date(use_end_time) as use_end_time')
				->where(['user_id'=>$userId]);
		}])->where(['member_id'=>$memberId])->where('source',0)->whereDate('created_at','=',date('Y-m-d'));
		return empty($live_id) ? $model->get() : $model->where(['live_id'=>$live_id])->get();
	}
}